package net.sh.dpinfrastructure.support.spring.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;

import net.sh.dpinfrastructure.cache.DPCache;
import net.sh.dpinfrastructure.cache.DPRedisCache;
import net.sh.dpinfrastructure.config.DPConfig;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 
 * 缓存配置类
 * 
 * @version 1.0
 * @since JDK1.7
 * @author shaohui
 * @company 上海朝阳永续信息技术有限公司
 * @copyright (c) 2016 SunTime Co'Ltd Inc. All rights reserved.
 * @date 2016年8月8日 下午2:05:13
 */
@Configuration
@Import(DPConfigConfiguration.class)
public class DPRedisCacheConfiguration {

    /**
     * 默认端口
     */
    public static final int DEFAULT_PORT = 6379;
    
    /**
     * 默认最大连接数
     */
    public static final int DEFAULT_POOL_MAX_TOTAL = 20;
    
    /**
     * 默认最大空闲连接数
     */
    public static final int DEFAULT_MAX_IDLE = 10;
    
    /**
     * 默认最小空闲连接数
     */
    public static final int DEFAULT_MIN_IDLE = 5;
    
    /**
     * 获取连接时的最大等待毫秒数
     */
    public static final long DEFAULT_MAX_WAIT_MILLIS = 60 * 1000;
    
    /**
     * 创建连接的时候是否测试
     */
    public static final boolean DEFAULT_TEST_ON_CREATE = true;
    
    /**
     * 申请连接的时候是否测试
     */
    public static final boolean DEFAULT_TEST_ON_BORROW = false;
    
    /**
     * 归还连接的时候是否测试
     */
    public static final boolean DEFAULT_TEST_ON_RETURN = false;
    
    /**
     * 资源回收线程执行一次回收操作，回收资源的数量
     */
    public static final int DEFAULT_NUM_TESTS_PER_EVICTION_RUN = 5;
    
    /**
     * 回收资源间隔
     */
    public static final int DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS = 60 * 1000;
    
    @Bean
    public JedisPool getJedisPool(DPConfig config) {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(config.getIntValue("redis.pool.maxTotal", DEFAULT_POOL_MAX_TOTAL));
        jedisPoolConfig.setMaxIdle(config.getIntValue("redis.pool.maxIdle", DEFAULT_MAX_IDLE));
        jedisPoolConfig.setMinIdle(config.getIntValue("redis.pool.minIdle", DEFAULT_MIN_IDLE));
        jedisPoolConfig.setMaxWaitMillis(config.getLongValue("redis.pool.maxWaitMillis", DEFAULT_MAX_WAIT_MILLIS));
        jedisPoolConfig.setTestOnCreate(config.getBoolValue("redis.pool.testOnCreate", DEFAULT_TEST_ON_CREATE));
        jedisPoolConfig.setTestOnBorrow(config.getBoolValue("redis.pool.testOnBorrow", DEFAULT_TEST_ON_BORROW));
        jedisPoolConfig.setTestOnReturn(config.getBoolValue("redis.pool.testOnReturn", DEFAULT_TEST_ON_RETURN));
        jedisPoolConfig.setNumTestsPerEvictionRun(config.getIntValue("redis.pool.testOnReturn", DEFAULT_NUM_TESTS_PER_EVICTION_RUN));
        jedisPoolConfig.setTimeBetweenEvictionRunsMillis(config.getLongValue("redis.pool.timeBetweenEvictionRunsMillis", DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS));
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, config.get("redis.address"), config.getIntValue("redis.port", DEFAULT_PORT), 
                config.getIntValue("redis.pool.maxWaitMillis", (int) DEFAULT_MAX_WAIT_MILLIS), config.get("redis.password"));
        return jedisPool;
    }
    
    @Bean(name = "redisCache", destroyMethod = "disconnect")
    @Primary
    public DPCache getDPRedisCache(JedisPool jedisPool) {
        return new DPRedisCache(jedisPool);
    }
}
